package com.it2297.view;

import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.border.EmptyBorder;

import com.it2297.renderer.BiddersListCellRenderer;

@SuppressWarnings("serial")
public class BiddersDetailDialog extends JDialog
{
	private final JScrollPane scrollPane = new JScrollPane();
	private final JList list = new JList();
	private final DefaultListModel listModel = new DefaultListModel();
	private final BiddersListCellRenderer listCellRenderer = new BiddersListCellRenderer();
	private final JPanel panel = new JPanel();
	private final JButton btnSelect = new JButton("Select");
	private final JButton btnClose = new JButton("Close");
	private final JLabel lblNewLabel = new JLabel("Choose a bidder to win the bid:");

	public BiddersDetailDialog()
	{

		initGUI();
	}

	private void initGUI()
	{
		setBounds(100, 100, 480, 320);
		setLocationRelativeTo(null);
		setModalityType(Dialog.ModalityType.APPLICATION_MODAL);

		getContentPane().add(scrollPane, BorderLayout.CENTER);
		list.setFocusable(false);
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		list.setModel(listModel);
		list.setCellRenderer(listCellRenderer);

		scrollPane.setViewportView(list);
		FlowLayout flowLayout = (FlowLayout) panel.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);

		getContentPane().add(panel, BorderLayout.SOUTH);

		panel.add(btnSelect);

		panel.add(btnClose);
		lblNewLabel.setBorder(new EmptyBorder(10, 10, 10, 10));

		getContentPane().add(lblNewLabel, BorderLayout.NORTH);
	}

	public void addListElement(Object object)
	{
		listModel.addElement(object);
	}

	public void clearList()
	{
		listModel.clear();
	}

	public void addSelectActionListener(ActionListener actionListener)
	{
		btnSelect.addActionListener(actionListener);
	}

	public void addCloseActionListener(ActionListener actionListener)
	{
		btnClose.addActionListener(actionListener);
	}

	public boolean isListSelectionEmpty()
	{
		return list.isSelectionEmpty();
	}

	public Object getSelectedBid()
	{
		// strip html tags
		// get only first 9 characters NRIC
		return list.getSelectedValue();
	}

}
